""" Script to import sections from text files

This script will read every .txt files in the
browsertests/specs/fixtures/spec_data/ directory and write a JSON file with
sections to browsertests/specs/fixtures/sections.json
"""
from __future__ import with_statement
import optparse
import re
import os
import logging

from django.core import serializers

log = logging.getLogger(__name__)
SECTION_LINE_RE = re.compile("(?:Appendix )?([\dA-Z][\d\.]*)\s*(.*)$")

def import_sections(spec, sections_filename):
    from browsertests.specs.models import Section

    i = 0
    sections = []
    all_numbers = set()
    with open(sections_filename) as sections_file:
        for line in sections_file:
            line = line.strip()
            if not line or line.startswith("#"):
                continue
            i += 1
            m = re.match("(?:Appendix )?([\dA-Z][\d\.]*)\s*(.*)$", line)
            if not m:
                raise Exception("Malformed section line: " + line)
            number, label = m.groups()
            assert number not in all_numbers, ("Duplicate number: %s for spec" %
                                               (number, spec))
            all_numbers.add(number)
            # TODO: sanity checks on the label (may not contain all kind of quotes)
            number = number.rstrip(".")
            log.debug("Handling section: spec:%s, number:%s, label:%s",
                      spec, number, label)

            section = Section()
            section.pk = "%s-%s" % (spec.pk, number)
            section.index = i
            section.number = number
            section.label = label
            section.spec = spec
            sections.append(section)
    log.info("Processed %i sections", i)
    return sections

def import_specs():
    from browsertests.specs.models import Spec

    IGNORED_SECTIONS_FILES = (
        "html4.txt", # Need to fix numbering
    )

    thisdir = os.path.dirname(os.path.abspath(__file__))
    spec_data_dir = os.path.join(thisdir, "fixtures", "spec_data")
    print spec_data_dir
    sections = []
    for f in os.listdir(spec_data_dir):
        if not f.endswith(".txt"):
            continue
        if f in IGNORED_SECTIONS_FILES:
            log.debug("File %s is ignored", f)
            continue
        log.info("Handling sections file %s", f)
        specid = os.path.splitext(f)[0]
        spec = Spec.objects.get(pk=specid)
        sections.extend(import_sections(spec, os.path.join(spec_data_dir, f)))

    json_filename = os.path.join(thisdir, "fixtures", "sections.json")
    with open(json_filename, "wb") as f:
        f.write(serializers.serialize("json", sections, indent=2))

    log.info("Sections written to %s", json_filename)

if __name__ == "__main__":
    from optparse import OptionParser
    usage = "%prog [options]"
    parser = OptionParser(usage=usage)
    parser.add_option('--settings', help='Python path to settings module, '
                      'e.g. "myproject.settings". If this isn\'t provided, '
                      'the DJANGO_SETTINGS_MODULE environment variable will'
                      ' be used.')
    parser.add_option('--debug',
        action="store_true", default=False,
        help='Debug mode')
    options, args = parser.parse_args()
    if options.settings:
        os.environ['DJANGO_SETTINGS_MODULE'] = options.settings
    elif "DJANGO_SETTINGS_MODULE" not in os.environ:
        parser.error("DJANGO_SETTINGS_MODULE is not set in the environment. "
                      "Set it or use --settings.")

    logging.basicConfig(level=(logging.DEBUG if options.debug else logging.INFO))

    import_specs()
